const articleModel = require('../../model/article/articleModel')
class articleService {
  //搜索文章
  async postSearchArticle(req,res){
    let query = req.body.query
    let pagenum = parseInt(req.body.pagenum)
    let pagesize = parseInt(req.body.pagesize)
    console.log('query='+query)
    console.log(pagenum)
    console.log(pagesize)
    if(pagesize === 1){
      pagenum = pagenum-1
    }else if(pagesize === 2){
      pagenum = (pagenum-1)*2
    }else if(pagesize === 5){
      pagenum = (pagenum-1)*5
    }else if(pagesize === 10){
      pagenum = (pagenum-1)*10
    }
    const {results:data1} = await articleModel.searchArticle(query,pagenum,pagesize)
    const {results:data2} = await articleModel.article()
    let datas = {
      'pagenum':pagenum,
      'total':data2.length,
      'article':data1,
    }
    if(datas.length != 0){
      return res.json({
        datas,
        meta: {
          msg: '查询文章成功',
          status: 200
        } 
      })
    }else{
      return res.json({
        meta: {
          msg: '查询文章失败',
          status: 404
        }
      })
    }
  }
  //查询文章列表
  async getArticle(req, res) {
    const { status, class_id, begin_pubdate, end_pubdate, response_type} = req.query
    let { page, per_page } = req.query
    
    console.log('page = ' + page)
    per_page = parseInt(per_page)
    console.log('per_page = ' + per_page)
    if (page == undefined) {
      page = 1
    }
    if(per_page == undefined){
      per_page = 10
    }
    let pagenum = null
    if (per_page === 1) {
      pagenum = page - 1
    } else if (per_page === 2) {
      pagenum = (page - 1) * 2
    } else if (per_page === 5) {
      pagenum = (page - 1) * 5
    } else if (per_page === 10) {
      pagenum = (page - 1) * 10
    }
    const { results: data } = await articleModel.queryArticle(status, class_id, begin_pubdate, end_pubdate, pagenum, per_page, response_type)
    let results = []

    data.forEach((item, i) => {
      data[i] = {
        id: data[i].id,
        title: data[i].title,
        status: data[i].status,
        pubdate: data[i].pubdate,
        author: data[i].author,
        class_id: data[i].class_id,
        content:data[i].content,
        cover: {
          images: data[i].images,
        }
      }
      return results.push(data[i])
    });
    const data1 = await articleModel.queryArticle(status, class_id, begin_pubdate, end_pubdate, response_type)
    const total_count = data1.results.length
    if (data != 0) {
      return res.json({
        data: {
          total_count,
          page,
          per_page,
          results
        }
      })
    }
  }

  //发布文章
  async postArticle(req,res){
    const {title,author,draft,content,images,class_id} = req.body
    console.log(images)
    console.log(title)
    console.log(author)
    console.log(draft)
    let status;
    if(draft){
      status = 3
    }else{
      status = 0
    }
    const {results:data} = await articleModel.publishArticle(title,author,status,content,images,class_id)
    if(data.length != 0){
      return res.json({
        data,
        meta: {
          msg: '发布文章成功',
          status: 200
        } 
      })
    }else{
      return res.json({
        meta: {
          msg: '发布文章失败',
          status: 404
        }
      })
    }
  }

  //修改文章状态
  async postUpdateStatus(req, res) {
    let status = null
    for(let i=0;i < 1323;i++){
      status =  Math.round(Math.random()*4)
      console.log(status )
      await articleModel.updateStatus(status,i)
    }
    return res.send('成功')
  }

  //查询用户发布文章
  async getUserArticle(req,res){
    const author = req.query.author
    const {results:data} = await articleModel.queryUserArticle(author)
    if(data.length != 0 ){
      return res.json({
        data,
        meta: {
          msg: '文章获取成功',
          status: 200
        } 
      })
    }else{
      return res.json({
        meta: {
          msg: '文章获取失败',
          status: 404
        } 
      })
    }
  }

  //查询id文章
  async getArticleID(req,res){
    const id = req.query.id
    const {results:data} = await articleModel.queryArticleID(id)
    if(data.length != 0){
      return res.json({
        data:data[0],
        meta: {
          msg: '文章获取成功',
          status: 200
        } 
      })
    }else{
      return res.json({
        meta: {
          msg: '文章获取失败',
          status: 404
        } 
      })
    }
  }

  //删除文章
  async postDleteArticle(req,res){
    let id = req.body.id
    const {results:data} = await articleModel.queryArticleID(id)
    if(data.length != 0 ){
      const deleteID = await articleModel.deleteArticleID(id)
      return res.json({
        data:data[0],
        meta: {
          msg: '删除文章成功',
          status: 200
        } 
      })
    }else{
      return res.json({
        meta: {
          msg: '删除文章失败',
          status: 200
        }
      })
    }
  }

  //修改文章
  async postUpdateArticle(req,res){
    let {title,author,content,images,class_id,id} = req.body
    const {results:data} = await articleModel.queryArticleID(id)
    if(data.length != 0){
      const update = await articleModel.updateArticle(title,author,content,images,class_id,id)
      res.json({
        data:data[0],
        meta:{
          msg: '文章修改成功',
          status: 200
        }
      })
    }else{
      res.json({
        meta:{
          msg: '文章修改失败',
          status: 404
        }
      })
    }
  }

  //统计作者文章发布数
  async getCountArticle(req,res){
    const {results:data} = await articleModel.countArticle()
    console.log(data)
    if(data.length !=0 ){
      return res.json({
        data,
        meta: {
          msg: '作者文章数获取成功',
          status: 200
        } 
      })
    }else{
      return res.json({
        meta: {
          msg: '作者文章数获取失败',
          status: 404
        } 
      })
    }
  }
}

module.exports = new articleService()